package vm;


import javafx.event.EventHandler;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

/**
 * Registro reikšmės redaktorius
 * @author Paulius Dambrauskas
 */
class EditingRegister extends TableCell<Register, String> {
 
      private TextField textField;
     
    
      @Override
      public void startEdit() {
          super.startEdit();
         
          if (textField == null) {
              createTextField();
          }
         
          setGraphic(textField);
          setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
          textField.selectAll();
      }
     
      @Override
      public void cancelEdit() {
          super.cancelEdit();
         
          setText(String.valueOf(getItem()));
          setContentDisplay(ContentDisplay.TEXT_ONLY);
      }
 
      @Override
      public void updateItem(String item, boolean empty) {
          super.updateItem(item, empty);
         
          if (empty) {
              setText(null);
              setGraphic(null);
          } else {
              if (isEditing()) {
                  if (textField != null) {
                      textField.setText(getString());
                  }
                  setGraphic(textField);
                  setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
              } else {
                  setText(getString());
                  setContentDisplay(ContentDisplay.TEXT_ONLY);
              }
          }
      }
 
      private void createTextField() {
          textField = new TextField(getString());
          textField.setMinWidth(this.getWidth() - this.getGraphicTextGap()*2);
          textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
              @Override
              public void handle(KeyEvent t) {
                  if (t.getCode() == KeyCode.ENTER) {
                      try{
                        System.out.println(Integer.parseInt(textField.getText().toString(), 16));  //ar 16tainis įvestas
                        commitEdit(textField.getText());
                      } catch(Exception e){
                          System.out.println("Bloga įvestis");
                         // e.printStackTrace();
                          //cancelEdit();
                      }
                  } else if (t.getCode() == KeyCode.ESCAPE) {
                      cancelEdit();
                  }
              }
          });
      }
     
      private String getString() {
          try {
              return formatValue(toHex(Integer.parseInt(this.getTableView().getItems().get(this.getIndex()).registerValue.get())));
          }
          catch (Exception e) {
              return formatValue(this.getTableView().getItems().get(this.getIndex()).registerValue.get());
          }
          //getItem() == null ? "" : formatValue(toHex(Integer.parseInt(getItem().toString())));
      }
      private String toHex(int s){
        return Integer.toHexString(s);
      }
      private String formatValue(String s){
          String size = "";
          for(int i = this.getTableView().getItems().get(this.getIndex()).registerSize; i > 0; i--)
              size += "0";
          return (size + s).substring(s.length()).toUpperCase();
                 // s.toUpperCase();
      }
  }
 

